package com.ruoyi.kis.controller;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult_KIS;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.annotaion.BasicAuthCheck;
import com.ruoyi.kis.domain.DispatchOrderF;
import com.ruoyi.kis.service.IKisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhanglm
 * @CreateDate: 2025-02-28 14:24
 * @FullNameClass: com.ruoyi.kis.controller.KisFhtzController
 * @Description: 销售出库单反馈
 * 发货通知单、退货通知单下发给wms，wms根据发货通知单给erp反馈销售出库单？
 */
@RestController
@RequestMapping("/kisxsckd")
@Api("kis接口")
public class KisXsckdController extends BaseController {



    @Autowired
    private IKisService kisService;

    private static final Logger LOGGER = LoggerFactory.getLogger(KisXsckdController.class);



    @ApiOperation("销售出库单反馈")
    @RequestMapping("/xsckdfeedback")
    @BasicAuthCheck
    public AjaxResult_KIS dispatchorderFeedback(@RequestBody @Valid DispatchOrderF dispatchOrderF) {
        LOGGER.info(JSONUtil.parse(dispatchOrderF).toString());

        if (dispatchOrderF == null || dispatchOrderF.getFROB() == null || dispatchOrderF.getFROB().isEmpty()) {
            return AjaxResult_KIS.error("error", "请求数据为空或上游单据类型为空");
        }

        String logPrefix = dispatchOrderF.getFROB().equals("1") ? "发货通知单反馈" : "退货通知单反馈";
        LOGGER.info("{}（销售出库单）给的值：{}", logPrefix, dispatchOrderF);

        Map<String, Object> map = kisService.insertXsckd(dispatchOrderF);
        LOGGER.info("{}（销售出库单）：{}", logPrefix, map);

        if (map == null || !"0".equals(map.get("MSGCODE"))) {
            return AjaxResult_KIS.error("error", map != null ? map.get("MSG").toString() : "操作失败");
        }
        return AjaxResult_KIS.success("success", map.get("MSG").toString());
    }

}
